<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css" />
<script type="text/javascript" th:src="@{/js/jquery.js}"></script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>采购单管理</li>
    <li>添加</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>采购单信息</span></div>
  <ul class="forminfo">
    <li>
      <label>采购单编号</label>
      <input name="purchaseId" type="text"  readonly="readonly" class="roinput" />
      <i>自动生成不能编辑</i>
    </li>
      <script>
          $(function () {
              SCPurchaseId();
          });
          function SCPurchaseId() {
              var date = new Date();
              $("input[name=purchaseId]").val("CG" + date.getTime());
          }
      </script>
    <li>
      <label>采购时间</label>
      <input name="purchaseTime" type="text" th:value="${#dates.format(session.date,'yyyy-MM-dd HH:mm:ss')}" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建人</label>
        <!--<input name="creatId" type="hidden" th:value="${session.currUser==null?'':session.currUser.UId}"/>-->
      <input  type="text" th:value="${session.currUser.uname}" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>审核状态</label>
      <input type="text" value="未审核" readonly="readonly" class="roinput" />
      <input name="checkStatus" type="hidden" value="1" />
      <i>初始状态，不能编辑</i>
    </li>
    <li>
      <label>总金额</label>
      <input name="totalMoney" type="text" readonly="readonly" class="roinput" />
       <i>自动运算，不能编辑</i>
    </li>
    <li>
      <input type="button"  id="xinzeng" value="新增"  class="smallbtn" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="删除" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="保存" class="smallbtn" onclick="addPur()"/>
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="返回" class="smallbtn" onclick="fanhui()"/>
    </li>
  </ul>

  <table class="tablelist">
      <thead>
        <tr>
          <th>序号</th>
          <th>品牌</th>
          <th>类型</th>
          <th>型号</th>
          <th>厂商</th>
          <th>数量</th>
          <th>单位</th>
          <th>单价</th>
          <th>金额</th>
        </tr>
      </thead>
      <tbody id="tbody">
      	<tr>
          <td >1</td>
          <td>
          	<select id="CBrand" name="CBrand" onchange="getCType(this.value)" >
            	<option value="0">请选择</option>
                <option th:each="b:${CBrand}" th:value="${b.brandId}" th:text="${b.brandName}"></option>
            </select>
          </td>
          <td>
          	<select id="CType" name="CType" onchange="getCProduct(this.value)">
            	<option value="0">请选择</option>
            </select>
          </td>
            <script type="text/javascript">
                function getCType(num) {
                    $.get("/erp1/purchase/purchase/ajaxType",{"bid":num},function (data) {
                        $("select[name=CType]").html("");
                        var options="<option value=''>请选择</option>";
                        $.each(data,function (i,e) {
                            options+="<option value='"+e.typeId+"'>"+e.typeName+"</option>";
                        });
                        $("select[name=CType]").append(options);
                    },"json");
                }
            </script>
          <td>
          	<select id="cProducts" name="cProducts" onchange="getCFirm(this.value),ajaxPrice(this.value)">
            	<option value="0">请选择</option>
            </select>
          </td>
            <script type="text/javascript">
                function getCProduct(num) {
                    $.get("/erp1/purchase/purchase/ajaxProduct",{"tid":num},function (data) {
                        $("select[name=cProducts]").html("");
                        var options="<option value=''>请选择</option>";
                        $.each(data,function (i,e) {
                            options+="<option value='"+e.productId+"'>"+e.productModel+"</option>";
                        });
                        $("select[name=cProducts]").append(options);
                    },"json");
                }
            </script>
          <td>
          	<select id="CFirm" name="CFirm">
            	<option value="0">请选择</option>
            </select>
          </td>
            <script type="text/javascript">
                function getCFirm(num) {
                        $.get("/erp1/purchase/purchase/ajaxFirm2",{"pid":num},function (data) {

                            $("select[name=CFirm]").html("");
                            var options="<option value=''>请选择</option>";
                            $.each(data,function (i,e) {
                                options+="<option value='"+e.firms.firmId+"'>"+e.firms.firmName+"</option>";
                            });
                            $("select[name=CFirm]").append(options);
                        },"json");

                }
            </script>
          <td><input id="count" type="text" name="count"  onchange="sum()"/></td>
          <td>台</td>
          <td><input  type="text" name="price"  onchange="sum()"/></td>
          <td><input  type="text" name="money"/></td>
        </tr>
      </tbody>
  </table>
</div>
<input type="hidden" id="price">
<script type="text/javascript">
    $(function () {
        $("#CBrand").blur(function () {
            ckCbrand();
        });
        $("#CType").blur(function () {
            ckCType();
        });
        $("#cProducts").blur(function () {
            ckcProducts();
        });
        $("#CFirm").blur(function () {
            ckCFirm();
        });
        $("#count").blur(function () {
            ckcount();
        });

    })
    function ckCbrand() {
        if($("#CBrand").val()==0){
            return false;
        }else {
            return true;
        }
    }
    function ckCType() {
        if($("#CType").val()==0){
            return false;
        }else {
            return true;
        }
    }
    function ckcProducts() {
        if($("#cProducts").val()==0){
            return false;
        }else {
            return true;
        }
    }
    function ckCFirm() {
        if($("#CFirm").val()==0){
            return false;
        }else {
            return true;
        }
    }
    function ckcount() {
        if($("#count").val()==0){
            return false;
        }else {
            return true;
        }
    }

    function addPur() {
        if(ckCbrand()&&ckCType()&&ckcProducts()&&ckCFirm()&&ckcount()){
            submitTo();
        }else {
            confirm("采购单管理添加错误！");
        }
    }

    function fanhui() {
        location.href="/erp/purchase/purchase/purchaseAll";
    }

    function submitTo() {
      var cBrands = $("select[name=CBrand]").val();
      var cTypes = $("select[name=CType]").val();
      var cProducts = $("select[name=cProducts]").val();
      var cFirms = $("select[name=CFirm]").val();
      var count = $("input[name=count]").val();
      var price = $("input[name=price]").val();
      var money = $("input[name=money]").val();
      if (cBrands != null && cTypes != null && cProducts != null && cFirms != null && count != null && price != null && money != null) {
        location.href = "/erp1/purchase/purchase/addPurchase?cBrands=" + cBrands + "&cTypes=" + cTypes + "&cProducts=" + cProducts +
                "&cFirms=" + cFirms + "&count=" + count + "&price=" + price + "&money=" + money;
      }
    }


    // 计算金额
    function sum(){
      var count = $("input[name=count]").val();  //商品数量
      var price = $("input[name=price]").val();  //商品价格
      if(count!=null){
        $("input[name=money]").val(count*price);
        $("input[name=totalMoney]").val(count*price);

      }
    }

    // 异步 得出商品价格
    function ajaxPrice(num){
      $.get("/erp1/purchase/purchase/ajaxPrice",{"productId":num},function (data) {
          if(data!=null){
              $("input[name=price]").val(data);
          }else{
              alert("null");
          }
      },"json");
    }

    $('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
